#!/user/bin/env python
# coding=utf-8

# 批量旋转图片
from PIL import Image
import os

"""
https://blog.csdn.net/wc19862274581/article/details/123554425
"""


def get_filelist(path):
    Filelist = []
    for home, dirs, files in os.walk(path):
        for filename in files:
            # 文件名列表，包含完整路径
            Filelist.append(os.path.join(home, filename))
            # # 文件名列表，只包含文件名
            # Filelist.append( filename)

    return Filelist


if __name__ == "__main__":
    # # 获取文件夹得目录
    # filePath = 'C:\\Users\\jx\\Desktop\\imgs\\touxiang_cs.jpg'
    # # 指定保存的文件夹
    # outputPath = 'C:\\Users\\jx\\Desktop\\imgs\\touxiang_cs2.jpg'
    #
    # # 获得文件夹下所有文件
    # Filelist = get_filelist(filePath)
    # print(len(Filelist))
    # # 迭代所有图片
    # for filename in Filelist:
    #     print(filename)
    #     # 读取图像
    #     im = Image.open(filename)
    #     # 指定逆时针旋转的角度
    #     # im_rotate = im.transpose(Image.ROTATE_90)
    #     im_rotate = im.transpose(Image.ROTATE_180)
    #     # im_rotate = im.transpose(Image.ROTATE_270)
    #     # 保存图像
    #     output_path = filename.replace(filePath, outputPath)
    #     outputdir = output_path.rsplit('\\', 1)[0]
    #     if not os.path.exists(outputdir):
    #         os.mkdir(outputdir)
    #     im_rotate.save(output_path)

    # 获取文件夹得目录
    filePath = 'C:\\Users\\jx\\Desktop\\imgs\\chaichaichai_cs.jpg'
    # 指定保存的文件夹
    outputPath = 'C:\\Users\\jx\\Desktop\\imgs\\chaichaichai_cs2.jpg'

    # 获得文件夹下所有文件
    # 迭代所有图片
    # for filename in Filelist:
    print(filePath)
    # 读取图像
    im = Image.open(filePath)
    # 指定逆时针旋转的角度
    # im_rotate = im.transpose(Image.ROTATE_90)
    # 镜像反转
    im_rotate = im.transpose(Image.FLIP_LEFT_RIGHT)
    # im_rotate = im.transpose(Image.ROTATE_270)
    # 保存图像
    output_path = filePath.replace(filePath, outputPath)
    outputdir = output_path.rsplit('\\', 1)[0]
    if not os.path.exists(outputdir):
        os.mkdir(outputdir)
    im_rotate.save(output_path)
